const DATE = new Date();
const date = {
	/**
	 * [格式化数字]
	 * @returns
	 */
	formatNumber(number: number) {
		return number < 10 ? `0${number}` : String(number);
	},

	/**
	 * [获取年]
	 * @returns
	 */
	getYear(value: any) {
		return value ? new Date(value).getFullYear() : DATE.getFullYear();
	},

	/**
	 * [获取月]
	 * @returns
	 */
	getMonth(value: any) {
		return value ? this.formatNumber(new Date(value).getMonth() + 1) : this.formatNumber(DATE.getMonth() + 1);
	},

	/**
	 * [获取天]
	 * @returns
	 */
	getDay(value: any) {
		return value ? this.formatNumber(new Date(value).getDate()) : this.formatNumber(DATE.getDate());
	},

	/**
	 * [根据年月获取天数]
	 * @returns
	 */

	getDaysByMonthAndYear(year: any, month: any) {
		const d: Date = new Date(year, parseInt(month, 10), 0);
		return d.getDate();
	},

	/**
	 * [获取星期几]
	 * @returns
	 */
	getWeek(value: any) {
		const week = value ? new Date(value).getDay() : DATE.getDay();
		let weekDesc = '';
		switch (week) {
			case 1:
				weekDesc += '星期一';
				break;
			case 2:
				weekDesc += '星期二';
				break;
			case 3:
				weekDesc += '星期三';
				break;
			case 4:
				weekDesc += '星期四';
				break;
			case 5:
				weekDesc += '星期五';
				break;
			case 6:
				weekDesc += '星期六';
				break;
			case 0:
				weekDesc += '星期日';
				break;
			default:
				break;
		}
		return weekDesc;
	},

	/**
	 * [获取周几]
	 * @returns
	 */
	getWeek1(value: any) {
		const week = value ? new Date(value).getDay() : DATE.getDay();
		let weekDesc = '';
		switch (week) {
			case 1:
				weekDesc += '周一';
				break;
			case 2:
				weekDesc += '周二';
				break;
			case 3:
				weekDesc += '周三';
				break;
			case 4:
				weekDesc += '周四';
				break;
			case 5:
				weekDesc += '周五';
				break;
			case 6:
				weekDesc += '周六';
				break;
			case 0:
				weekDesc += '周日';
				break;
			default:
				break;
		}
		return weekDesc;
	},

	// 时间戳转化为时间
	happenTimeFun(num: any) {
		const dt: Date = new Date(num);
		// 时间戳为10位需*1000，时间戳为13位的话不需乘1000
		const y = dt.getFullYear();
		let MM: any = dt.getMonth() + 1;
		// 月补0
		MM = MM < 10 ? `0${MM}` : MM;
		let d: any = dt.getDate();
		// 天补0
		d = d < 10 ? `0${d}` : d;
		let h: any = dt.getHours();
		// 小时补0
		h = h < 10 ? `0${h}` : h;
		let m: any = dt.getMinutes();
		// 分钟补0
		m = m < 10 ? `0${m}` : m;
		let s: any = dt.getSeconds();
		// 秒补0
		s = s < 10 ? `0${s}` : s;
		return `${y}-${MM}-${d} ${h}:${m}:${s}`;
	},
	// 新增资讯发布时间计算
	createTime(d: string): string {
		const time = Date.now() - Date.parse(d);
		// 获取天数
		const day = Math.floor(time / (1000 * 60 * 60 * 24));
		// 获取分钟
		const minute = Math.floor((time / (1000 * 60)) % 60);
		// 获取小时
		const hour = Math.floor((time / (1000 * 60 * 60)) % 24);
		if (!day && hour) {
			return `${hour}小时前`;
		}
		if (!hour && !day && minute) {
			return `${minute}分钟前`;
		}
		if (day <= 7) {
			// eslint-disable-next-line no-nested-ternary
			return day === 1 ? '昨天' : day === 2 ? '前天' : `${day}天前`;
		}
		return d;
	}
};

export default date;
